package com.cybersoft.jdbc.gavin;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

import com.cybersoft.jdbc.gavin.dao.IStudentDao;
import com.cybersoft.jdbc.gavin.dao.impl.StudentDao;
import com.cybersoft.jdbc.gavin.entity.Student;
import com.cybersoft.jdbc.gavin.util.BaseDao;

/**
 * Purpose: 測試jdbc
 * @author cybersoft
 * @since  JDK 1.5
 * @date   2013/4/11
 * @MaintenancePersonnel cybersoft
 */
public class TestJdbc {
	public static IStudentDao dao ;
	public static Scanner in =  new Scanner(System.in);
	   
	/**
	 * Constructor:
	 */
	public TestJdbc() {
	}

	/**
	 * Purpose:
	 * @param args
	 * @return void
	 */
	public static void main(String[] args) {
		
		 dao = new StudentDao();
		boolean isExit = false;
		
		
		
		while(!isExit){
			System.out.println("歡迎使用學員信息管理系統,請選擇要執行的操作");
			System.out.println("0 退出");
			System.out.println("1 查看所有成員");
			System.out.println("2 增加學員");
			System.out.println("3 修改學員");
			System.out.println("4 刪除學員");
			System.out.println("5 清空數據");
			System.out.println("6 創建表");
			System.out.println("7 插入數據");
			System.out.print("請輸入序號：");
		
			int intOption = in.nextInt();
			switch (intOption) {
			case 0:
				isExit= true ;
				System.out.println("謝謝使用，再見！");
				break;
			case 1:
				printAllUser();
				break;
			case 2:
				addStudent();
				break;
			case 3:
				doUpdate();
				break;
			case 4:
				 doDel();
				break;
			case 5:
				 dao.dropTable("SEVEN.Student_gavin");
				break;
			case 6:
				dao.createTable();
				break;
			case 7:
				dao.init();
				break;
			default:
				break;
			}
			
			System.out.println("----------------------------------------------");
			System.out.println();
			
			
			
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	//  dao.createTable();  屈沛沛,gavin,12,100,13991221139,6,7
		 
	/* 	
	 	Student stu = new Student();
		stu.setStrStuName("屈沛");
		stu.setStrStuEnName("gavin");
		System.out.println(dao.addStudent(stu));   
		*/
		
		
	 //	dao.dropTable("Student_gavin");
	 	
	// 	System.out.println("刪除數據"+dao.delStudentByName("屈沛2"));
		
	
	 	
	 	//修改
 	/*	Student stu = new Student();
		stu.setStrStuName("屈沛2");
		stu.setStrStuEnName("gavin");
		stu.setStrStuSex("n");
		stu.setIntStuAge(12);
		stu.setDStuHeight(1);
		System.out.println("更新："+dao.updateStudent(stu));
 	*/
		
		 
		 
	}
	
	
	private static void doDel() {
		Student stu  = null;
		System.out.println("請輸如要刪除的學員姓名:");
		String str = in.next();
		 if(dao.delStudentByName(str)==1){
			 System.out.println("學員"+str+" 刪除成功");
		 }else{
			 System.out.println("刪除失敗！");
		 }
	}

	public static void printAllUser(){
		
		List<Student> stuList = dao.getAllStudent();
		for (Student student : stuList) {
			System.out.println(student.toString());
		}
		
		
	}
	
	public static void addStudent(){
		Student stu  = null;
		System.out.println("請輸入學員信息，格式如【姓名,英文名,性別,年齡,2013-04-11格式生日,身高,電話,email】");
		String str = in.next();
		str = str.replace("，", ",");
		String[] stuInfoArr = str.split(",");
		if(stuInfoArr.length!=8){
			System.out.println("信息格式有誤");
			return;
		}else{
			
			try {
				stu = new Student(stuInfoArr[0],stuInfoArr[1],stuInfoArr[2],Integer.parseInt(stuInfoArr[3]),java.sql.Date.valueOf(stuInfoArr[4]),Double.parseDouble(stuInfoArr[5]),stuInfoArr[6],stuInfoArr[7]);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("輸入有誤");
			} 
			
		}
		int intRe = dao.addStudent(stu);
		if(intRe!=1){
			System.out.println("增加失敗，請重試");
		}else{
			System.out.println("增加成功！");
		}
		
	}
	
	
	public static void doUpdate(){
		Student stu  = null;
		System.out.println("請輸入要求該的學員的信息，格式如【姓名,英文名,性別,年齡,2013-04-11格式生日,身高,電話,email】，則修改張三的個人信息");
		String str = in.next();
		str = str.replace("，", ",");
		String[] stuInfoArr = str.split(",");
		if(stuInfoArr.length!=8){
			System.out.println("信息格式有誤");
			return;
		}else{
			
			try {
				stu = new Student(stuInfoArr[0],stuInfoArr[1],stuInfoArr[2],Integer.parseInt(stuInfoArr[3]),java.sql.Date.valueOf(stuInfoArr[4]),Double.parseDouble(stuInfoArr[5]),stuInfoArr[6],stuInfoArr[7]);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("輸入有誤");
			} 
			
		}
		int intRe = dao.updateStudent(stu);
		if(intRe!=1){
			System.out.println("修改失敗，請重試");
		}else{
			System.out.println("修改成功！");
		}
		
	}

}
